package userManagement;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

import db.DatabaseConnector;

public class ListUsers extends HttpServlet {

//doPost method included for POST requests
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

//This subroutine is called from SubmitQ.jsp
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        ResultSet rs;

	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	DatabaseConnector DBConnect = new DatabaseConnector(DatabaseConnector.getDatabaseType());

        String SQL = "select U.username, U.password, I.email from USERS U, USER_INFO I "+
                     "where U.username=I.username ORDER BY username";
        rs = DBConnect.selectSQL_RS(SQL);

        out.println("<html><head>");

	out.println("<script type=\"text/javascript\">");
	out.println("function message(username){");
	out.println("var ans = confirm(\"Are you sure you want to delete user '\"+username+\"'?\");");
	out.println("if (ans==true){");
	out.println("document.location.href = \"DelUser?username=\"+username;");
	out.println("} }");
	out.println("</script>");

	out.println("</head><body>");

	out.println("<table border=\"1\" cellpadding=\"3\">");
	out.print("<tr><td bgcolor=\"c0c0c0\">Username</td>");
	out.println("<td bgcolor=\"c0c0c0\">E-mail</td>");
	out.println("<td bgcolor=\"c0c0c0\">Control</td></tr>");

	try {
            if(rs != null) {
                while(rs.next())
                {
                    out.print("<tr>");
                    out.print("<td>" + rs.getString("username") + "</td>");
                    out.print("<td>" + rs.getString("email") + "</td>");
                    out.print("<td><form method=\"post\" action=\"addUser\" style=\"margin: 0px; display:inline;\">");
                    out.println("<input type=\"hidden\" name=\"username\" value=\""+rs.getString("username")+"\" />");
                    out.println("<input type=\"submit\" value=\"Edit\" onclick=\"this.form.action='EditUser?username="+rs.getString("username")+"&loadCase=edit'\" /></form>");
                    out.println("<input type=\"submit\" value=\"Delete\" onclick=\"message('" + rs.getString("username") + "')\" /></td></tr>");
                }
            }
        } catch (SQLException e) {
            out.println("SQLException caught in ChronoList2: " + e.getMessage());
        } finally {
            try {
                rs.close();
                DBConnect.CleanUp();
            } catch (Exception ignored) {}
        }
	out.println("</table>");

        out.print("<br /><form method=\"post\" action=\"AddUser?loadCase=add\" style=\"margin: 0px; display:inline;\">");
	out.println("<input type=\"submit\" value=\"Add New User\" /></form><br /><br />");
        out.println("<a href=\"index.jsp\">Return to Website Administration</a>");

	out.println("</body></html>");
    }
}

